package com.meilai.project.service.finance.supplier.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.dto.finance.supplier.SupplierMaterialCalDTO;
import com.meilai.project.dto.finance.supplier.SupplierMaterialListDTO;
import com.meilai.project.entity.finance.supplier.SupplierMaterial;
import com.meilai.project.exception.CommonException;
import com.meilai.project.mapper.finance.supplier.SupplierMaterialMapper;
import com.meilai.project.service.finance.supplier.SupplierMaterialService;
import com.meilai.project.util.StringChangeUtil;
import com.meilai.project.vo.finance.supplier.SupplierMaterialWebVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SupplierMaterialServiceImpl extends ServiceImpl<SupplierMaterialMapper, SupplierMaterial> implements SupplierMaterialService {


    @Override
    public Page<SupplierMaterialWebVO> selectPageList(SupplierMaterialListDTO searchDTO) {
        Page<SupplierMaterialWebVO> page = searchDTO.createPage(SupplierMaterialWebVO.class);
        List<SupplierMaterialWebVO> listResult = baseMapper.selectPageList(page, searchDTO);
        page.setRecords(listResult);
        return page;
    }

    @Override
    public boolean edit(SupplierMaterial dto) {
        Long id = dto.getId();
        SupplierMaterial temp = getById(id);
        if (temp == null) return false;
        List<SupplierMaterial> list = baseMapper.selectList(new QueryWrapper<SupplierMaterial>().eq("supplier_id", temp.getSupplier_id())
                .eq("material_texture_id", dto.getMaterial_texture_id()).ne("id", id).isNull("deleted_at"));
        if (list != null && list.size() > 0) throw new CommonException("供应商已存在该材质，请重新选择材质");
        SupplierMaterial material = new SupplierMaterial();
        material.setId(id);
        material.setMaterial_texture_id(dto.getMaterial_texture_id());
        material.setPrice(dto.getPrice());
        return updateById(material);
    }

    @Override
    public boolean delete(Long id) {
        SupplierMaterial temp = getById(id);
        if (temp == null) return false;
        SupplierMaterial material = new SupplierMaterial();
        material.setId(id);
        material.setDeleted_at(LocalDateTime.now());
        return updateById(material);
    }

    @Override
    public void calTotal(SupplierMaterialCalDTO dto) {
        List<SupplierMaterial> ms = list(Wrappers.<SupplierMaterial>lambdaQuery()
                .eq(SupplierMaterial::getSupplier_id, dto.getSupplier_id())
                .eq(SupplierMaterial::getMaterial_texture_id, dto.getMaterial_texture_id())
                .isNull(SupplierMaterial::getDeleted_at));
        if (CollectionUtils.isNotEmpty(ms)) {
            SupplierMaterial temp = ms.get(0);
            String width = dto.getSize_width();
            String height = dto.getSize_height();
            if ((StringChangeUtil.isInteger(width) || StringChangeUtil.isDouble(width))
                    && (StringChangeUtil.isInteger(height) || StringChangeUtil.isDouble(height))
                    && temp.getPrice() != null) {
                dto.setPrice(temp.getPrice());
                BigDecimal total = new BigDecimal(dto.getSize_width())
                        .multiply(new BigDecimal(dto.getSize_height()))
                        .multiply(temp.getPrice())
                        .multiply(new BigDecimal(dto.getNum().toString()))
                        .divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP);
                dto.setTotal(total);
            }
        }
    }
}
